package club.yuit.desktop.ui.topics;

import club.yuit.desktop.layout.WrapLayout;
import club.yuit.desktop.ui.UITools;
import club.yuit.desktop.ui.producer.ProducerUI;
import club.yuit.desktop.ui.consumer.ConfigConsumerDialog;
import club.yuit.rmq.model.TopicDetails;
import club.yuit.rmq.service.RMQClusterService;
import club.yuit.rmq.service.ProducerService;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

/**
 * @author: yuit
 * @date: 2023/11/28 13:24
 */
@Slf4j
public class TopicCardListPanel  extends JPanel {

    private String currentTopic;

    private ProducerService producerService;

    private final JPopupMenu menu;



    public TopicCardListPanel() {
        this.setLayout(new WrapLayout(FlowLayout.LEFT));
        this.producerService = new ProducerService();
        menu = createMenu();
    }


    /**
     * 更新数据和UI
     * @param service
     */
    public void updateTopicsUI(RMQClusterService service){

        try {
            List<TopicDetails> ls = service.getTopics();

            this.removeAll();
            for (TopicDetails details:ls){
                TopicCardPanel panel = new TopicCardPanel(details, this);
                panel.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        int button = e.getButton();
                        // 鼠标右键
                        if(button==MouseEvent.BUTTON3){
                            Component component = e.getComponent();
                            JPanel source = (JPanel) e.getSource();
                            currentTopic =  source.getName();
                            if(!menu.isShowing()){
                                menu.show(component,e.getX(),e.getY());
                            }else {
                                menu.setVisible(false);
                            }
                        }
                    }
                });
                this.add(panel);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 内边距
     * @return
     */

    @Override
    public Insets getInsets() {
        return new Insets(0,10,10,10);
    }

    private JPopupMenu createMenu(){
        JPopupMenu menu = new JPopupMenu();
        JMenuItem producer = new JMenuItem("生产者");
        producer.setIcon(UITools.getSVGIcon("/imgs/building.svg", 16,16));
        JMenuItem consumer = new JMenuItem("消费者");
        consumer.setIcon(UITools.getSVGIcon("/imgs/download.svg", 16,16));
        JMenuItem details = new JMenuItem("详细信息");
        details.setIcon(UITools.getSVGIcon("/imgs/infos.svg", 16,16));
        JMenuItem del = new JMenuItem("删除");
        del.setIcon(UITools.getSVGIcon("/imgs/delete.svg", 16,16));
        menu.add(producer);
        menu.add(consumer);
        menu.add(details);
        menu.add(del);


        producer.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                new ProducerUI(getTopLevelAncestor(),currentTopic, producerService);
            }
        });

        consumer.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                // 获取当前JPanel 的顶级组件
                Container topLevelAncestor = getTopLevelAncestor();
                ConfigConsumerDialog dialog = new ConfigConsumerDialog(currentTopic,topLevelAncestor);
                menu.setVisible(false);
                dialog.showDialog();

            }
        });

        details.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println(currentTopic+"[details]");
            }
        });

        del.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println(currentTopic+"[del]");
            }
        });
        return menu;
    }


    /**
     * 过滤显示
     * @param showTypeValue
     * @param cluster
     */
    public void updateShowByTypeValue(int showTypeValue,String cluster){

        Component[] components = this.getComponents();
        for (Component c: components){
            if(c instanceof TopicCardPanel tcp){
                boolean show = tcp.isShow(showTypeValue);
                if (cluster!=null){
                    boolean contains = tcp.getDetails().getCluster().contains(cluster);
                    show &= contains;
                }
                c.setVisible(show);
            }
        }
    }

    public void setCurrentTopic(String currentTopic) {
        this.currentTopic = currentTopic;
    }


    public JPopupMenu getMenu() {
        return menu;
    }
}
